package com.yvdelx.cyclowomen.utility;

import android.app.Dialog;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.yvdelx.R;
import com.yvdelx.cyclowomen.object.Day;
import com.yvdelx.cyclowomen.object.EnumNature;
import com.yvdelx.cyclowomen.object.EnumSensation;

/**
 * View utilities
 * Created by ydelvaux on 21/02/14.
 */
public class ViewUtility {

    /**
     * Put a String into a Text View widget
     * @param textView View
     * @param text String
     */
    public static void setTextInTextView(View textView, String text){
        if(text != null)
            ((TextView) textView).setText(text.toCharArray(), 0, text.length());
        else
            ((TextView) textView).setText("".toCharArray(), 0, "".length());
    }

    /**
     * Put a String from R.id into a Text View widget
     * @param textView View
     * @param idR String
     */
    public static void setTextInTextView(View textView, int idR){
        ((TextView) textView).setText(idR);
    }

    /**
     * Put a String from R.id into a Text View widget
     * @param editText View
     * @param text String
     */
    public static void setTextInEditText(View editText, String text){
        ((EditText) editText).setText(text);
    }

    /**
     * Return the id text of the nature
     * @param day Day
     * @return Integer
     */
    public static Integer getNature(Day day){
        if(day.getNature() == EnumNature.HIGH_POINT){
            return R.string.HighPoint;
        } else if(day.getNature() == EnumNature.LOW_POINT) {
            return R.string.LowPoint;
        } else {
            return R.string.undetermined;
        }
    }

    /**
     * Return  the id text of the sensation
     * @param day Day
     * @return Integer
     */
    public static Integer getSensation(Day day){
        if(day.getSensation() == EnumSensation.DRY){
            return R.string.Dry;
        } else if(day.getSensation() == EnumSensation.HUMID) {
            return R.string.Humid;
        } else if(day.getSensation() == EnumSensation.WET) {
            return R.string.Wet;
        } else if(day.getSensation() == EnumSensation.MENSES) {
            return R.string.Menses;
        } else {
            return R.string.Lubricate;
        }
    }

   public static String getStringEditText(View editText){
       //noinspection ConstantConditions
       return ((EditText) editText).getText().toString();
   }

    public static Float getLongEditText(View editText){
        return Float.valueOf(getStringEditText(editText));
    }

    public static void setViewOnClickListener(View view, View.OnClickListener listener){
        view.setOnClickListener(listener);
    }


    /**
     * Set the selected item at the string in param
     * @param spinner Spinner
     * @param stringItem String
     */
    public static void setSelectItemOnSpinner(View spinner, String stringItem) {
        ArrayAdapter adapter = (ArrayAdapter) ((Spinner)spinner).getAdapter();
        // Get all items in the spinner
        for (int position = 0; position < adapter.getCount(); position++) {
            // If the item searched is found, update the spinner and stop the loop.
            if(String.valueOf(adapter.getItem(position)).equals(stringItem)){
                ((Spinner)spinner).setSelection(position);
                break;
            }
        }
    }

    /**
     * Get the num of star filled in a rating bar
     * @param ratingBar View
     * @return float
     */
    public static float getNumStarFilled(View ratingBar) {
        return ((RatingBar) ratingBar).getRating();
    }

    /**
     * Set the rating on a rating bar
     * @param ratingBar View
     * @param value Float
     */
    public static void setRatingOnRatingBar(View ratingBar, Float value) {
        if(value != null)
            ((RatingBar) ratingBar).setRating(value);
        else
            ((RatingBar) ratingBar).setRating(0f);
    }

    /**
     * Get the value of the checkbox
     * @param checkBox View
     * @return Boolean
     */
    public static boolean getValueCheckBox(View checkBox) {
        return ((CheckBox) checkBox).isChecked();
    }

    /**
     * Set the value of the check box
     * @param checkBox View
     * @param value Boolean
     */
    public static void setValueCheckBox(View checkBox, Boolean value) {
        ((CheckBox) checkBox).setChecked(value);
    }

    /**
     * Get the radio group in a dialog
     * @param dialog Dialog
     * @return RadioGroup
     */
    public static RadioGroup getRadioGroup(Dialog dialog) {
        return (RadioGroup) dialog.findViewById(R.id.radioGroupCycleChoice);
    }
}
